package com.cykj.shop.controller;

import com.cykj.common.core.domain.R;
import com.cykj.common.core.web.controller.BaseController;
import com.cykj.common.core.web.domain.AjaxResult;
import com.cykj.common.security.annotation.RequiresPermissions;
import com.cykj.shop.service.IShopService;
import com.cykj.shop.service.ShopService;
import com.cykj.shop.domain.vo.ShopVo;
import com.cykj.system.api.RemoteFileService;
import com.cykj.system.api.domain.Shop;
import com.cykj.system.api.domain.SysFile;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.sql.Time;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.util.List;

/**
 * @author 叶佳伟
 * @date 2025/8/18
 * @description 功能描述: 店铺控制器
 */

@RestController
@RequestMapping("/shopInfo")
public class ShopController {
    @Autowired
    private ShopService shopService;
    @Resource
    private RemoteFileService remoteFileService;


    //商家店铺入驻
    @PostMapping("addShop")
    public AjaxResult addShop(
            @RequestParam Integer businessId,
            @RequestParam String shopName,
            @RequestParam String shopAddress,
            @RequestParam Integer shopCategory,
            @RequestParam String shopStart,
            @RequestParam String shopEnd,
            @RequestParam MultipartFile shopAvatar,
            @RequestParam MultipartFile shopLicense,
            @RequestParam MultipartFile foodSafe,
            @RequestParam MultipartFile shopImage,
            @RequestParam MultipartFile idCard
    ) {

        // 文件上传
        R<SysFile> shopAvatarFile = remoteFileService.upload(shopAvatar);
        R<SysFile> shopLicenseFile = remoteFileService.upload(shopLicense);
        R<SysFile> foodSafeFile = remoteFileService.upload(foodSafe);
        R<SysFile> shopImageFile = remoteFileService.upload(shopImage);
        R<SysFile> idCardFile = remoteFileService.upload(idCard);

        //获取身份证上传地址
        String idCardPath = idCardFile.getData().getPath();
        //使用身份证磁盘地址进行ocr


        //获取图片Url
        String shopAvatarUrl = shopAvatarFile.getData().getUrl();
        String shopLicenseUrl = shopLicenseFile.getData().getUrl();
        String foodSafeUrl = foodSafeFile.getData().getUrl();
        String shopImageUrl = shopImageFile.getData().getUrl();

        ShopVo shopVo = new ShopVo(businessId,shopName,shopAddress,shopCategory,shopAvatarUrl,shopImageUrl,shopLicenseUrl,foodSafeUrl,shopStart,shopEnd);

        System.out.println(shopVo);

        //接下来使用shopVo做存入数据库的操作(接口是service层的addShop，实现还没写)

        return null;
    }

    @GetMapping("/list")
    public AjaxResult getListShop(ShopVo shopVo) {

        return shopService.getListShop(shopVo);
    }


    //商家店铺信息展示
    @RequiresPermissions("shop:shopInfo:showShopInfo")
    @GetMapping("showShopInfo")
    public AjaxResult showInfo(@RequestParam Integer businessId) {
        return shopService.showInfo(businessId);
    }

    //店铺营业状态切换
    @PutMapping("updateStatus")
    public AjaxResult updateStatus(@RequestBody ShopVo shopVo) {
        return shopService.updateStatus(shopVo);
    }

    //修改店铺信息
    @PutMapping("updateInfo")
    public AjaxResult updateInfo(@RequestBody ShopVo shopVo) {
        return shopService.updateInfo(shopVo);
    }

    //店铺头像修改
    @PutMapping("updateAvatar")
    public AjaxResult updateAvatar(@RequestParam Integer id,
                                   @RequestParam MultipartFile shopAvatar){

        //文件上传
        R<SysFile> shopAvatarFile = remoteFileService.upload(shopAvatar);
        //获取图片Url
        String shopAvatarUrl = shopAvatarFile.getData().getUrl();
        return shopService.updateAvatar(id,shopAvatarUrl);
    }
    // 根据商家ID获取店铺ID
    @GetMapping("/getShopIdByBusinessId")
    public AjaxResult getShopIdByBusinessId(@RequestParam Integer businessId) {
        return shopService.getShopIdByBusinessId(businessId);
    }

}
